package com.wdt.data.controller;

import com.alibaba.fastjson.JSONException;
import com.wdt.data.model.SysUser;
import com.wdt.data.service.SysUserService;
import com.wdt.data.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ProvienceLogin {
    @Autowired
    SysUserService userService;

    @RequestMapping(value = "/Authorization/PostAuto", method = RequestMethod.POST)
    public Map redict(HttpServletRequest request, HttpServletResponse response) {
        Map result = new HashMap();
        try {
            result.put("IsSuccess", false);
           /* if (StringUtils.isEmpty(json)) {
                result.put("Message", "参数不存在");
                return result;
            }
            Map jsonMap = JsonUtil.fromJson(json, Map.class);*/
            String appID = request.getParameter("appID");
            String appSecret = request.getParameter("appSecret");
            if (StringUtils.isEmpty(appID) || StringUtils.isEmpty(appSecret)) {
                result.put("Message", "用户名或密码不可为空");
                return result;
            }
            SysUser user = userService.getUserByAccount(appID);
            if (user == null) {
                result.put("Message", "无此用户");
                return result;
            }
            if (!user.getPassword().equals(appSecret)) {
                result.put("Message", "密码错误");
                return result;
            }

            response.sendRedirect("http://111.62.41.245:8090/login_provience.html?username=" + appID + "&pwd=" + appSecret);
        } catch (JSONException e) {
            result.put("Message", "Json字符串格式化异常");
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return result;
    }
}
